package com.seari.task;

import static org.hamcrest.CoreMatchers.nullValue;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.seari.bean.FaultInfo;
import com.seari.bean.LineInfo;
import com.seari.bean.StationInfo;
import com.seari.commonutils.redis.JedisUtilProxy;
import com.seari.commonutils.redis.ex.JedisNotInitializeException;
import com.seari.main.StartUpClass;
import com.seari.service.FaultService;
import com.seari.service.LineService;
import com.seari.service.StationService;
import com.seari.utils.HandlerNullUtils;

import redis.clients.jedis.Jedis;

@Component
@PropertySource("classpath:cron.props")
public class FaultSyncTask {
	
    private static final Logger log = LoggerFactory.getLogger(StartUpClass.class);
    	
	@Autowired
	private FaultService faultService;
	
    @Autowired
    private StationService stationService;
    
    @Autowired
    private LineService lineService;
    
	@Scheduled(cron="${jobs.schedule}")
	public void todayFaultSyncFromOracleToMysql() {
		log.info("======= 定时器执行  faultTodaySyncFromOracleToMysql =======");
		
		List<FaultInfo> faultInfoListOracle = new ArrayList<FaultInfo>();
		faultInfoListOracle = faultService.getTodayFaultInfoFromOracle();

		List<FaultInfo> faultInfoListMysql = new ArrayList<FaultInfo>();
		faultInfoListMysql = faultService.getTodayFaultInfoFromMysql();
		
		List<FaultInfo> faultInfoList = new ArrayList<FaultInfo>();
		if(!HandlerNullUtils.objectIsNull(faultInfoListOracle)) {
			if(HandlerNullUtils.objectIsNull(faultInfoListMysql)) {
				for(FaultInfo faultInfo:faultInfoListOracle) {
					//2 表示故障创建
					faultInfo.setFaultStatu("2");
					//1表示小电科故障 信息 
					faultInfo.setDataType("1");
					faultInfoList.add(faultInfo);
				}
			}else {
				for(FaultInfo faultInfo:faultInfoListOracle) {
					for(FaultInfo fault:faultInfoListMysql) {
						//修改
						if(faultInfo.getFaultId().equals(fault.getFaultId())) {
							if(!faultInfo.getFaultContent().equals(fault.getFaultContent())
									||!faultInfo.getFaultLine().equals(fault.getFaultLine())
									||!faultInfo.getFaultLocation().equals(fault.getFaultLocation())
									||!faultInfo.getUserId().equals(fault.getUserId())
									||!faultInfo.getUserName().equals(fault.getUserName())
									||!faultInfo.getLeaderPhone().equals(fault.getLeaderPhone())) {
								faultInfoList.add(faultInfo);
							}
						//新增
						}else {
							//2 表示故障创建
							faultInfo.setFaultStatu("2");
							//1表示小电科故障 信息 
							faultInfo.setDataType("1");
							faultInfoList.add(faultInfo);
						}
					}
				}
			}
		}
		if(!HandlerNullUtils.objectIsNull(faultInfoList)) {

			faultService.addOrUpdateFaultInfoToMysql(faultInfoList);
		}

		log.info("======= 定时器执行结束  faultTodaySyncFromOracleToMysql =======");

	}
	
	@Scheduled(cron="${jobs.schedule.redis}")
	public void syncSqlServerDataToRedis() {
		
		Jedis jedis = null;

		List<LineInfo> lineInfoList = lineService.getLineListInfoFromJuqun();
		List<StationInfo> stationInfoList = stationService.getStationInfoList();

		try {
			jedis = JedisUtilProxy.getJedis();
		} catch (JedisNotInitializeException e) {
			e.printStackTrace();
		}
		String lineObjectJson = JSON.toJSONString(lineInfoList);
		jedis.set("lineInfoList", lineObjectJson);

		String stationObjectJson = JSON.toJSONString(stationInfoList);
		jedis.set("stationInfoList", stationObjectJson);

		jedis.close();
	}
}
